<template>
	<view class="wrap">
		<view class="pic">
			<image src="https://ya30images.hbwlife.com/wxmp/subMessage.png" mode=""></image>
		</view>
		<view class="tips">
			订阅消息可及时接收审核结果
		</view>
		<view class="btn" @click="fnClick">
			订阅消息
		</view>
	</view>
</template>



<script>
	import {
		request,
		requestData
	} from '@/utils/http.js'
	export default {
		data() {
			return {

			}
		},
		onLoad(option) {},
		onShow() {},
		onUnload() {

		},
		methods: {
			fnClick() {
				const _this = this;
				// 获取用户的当前设置，判断是否点击了“总是保持以上，不在询问”
				wx.getSetting({
					withSubscriptions: true, // 是否获取用户订阅消息的订阅状态，默认false不返回
					success(res) {
						console.log('res.authSetting', res.authSetting)
						if (res.authSetting['scope.subscribeMessage']) {
							console.log('用户点击了“总是保持以上，不再询问”')
						} else {
							console.log('用户点击“总是保持以上，不再询问”则每次都会调起订阅消息')
							//因为没有选择总是保持，所以需要调起授权弹窗再次授权
							_this.subMessage();
						}
					}
				})
			},
			subMessage() {
				let tmplIds = 'UzO7UGyTfNpBz-sNcp_PHpZptQT8rDPPj0SjOqS0ZC4';
				wx.requestSubscribeMessage({
					tmplIds: [tmplIds], //消息模板ID
					success(res) {
						//用户同意订阅消息
						console.log('用户同意订阅消息', res)
						if (res.errMsg === 'requestSubscribeMessage:ok') {
							if (res[tmplIds] === 'accept') {
								uni.showToast({
									title: '订阅成功',
									icon: 'none',
								})
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/my/index',
									})
								}, 2000)
							} else if (res[tmplIds] === 'reject') {
								uni.showToast({
									title: '订阅失败',
									icon: 'none',
								})
							}
						}
					},
					fail(err) {
						//用户拒绝订阅消息
						console.log('用户拒绝订阅消息', err)
						uni.showToast({
							title: '订阅失败',
							icon: 'none',
						})
					},
				})
			}
		}
	}
</script>

<style scoped>
	.wrap {
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		background: #fff;
		font-size: 32rpx;
		display: flex;
		flex-flow: column;
		align-items: center;
	}

	.pic {
		width: 160rpx;
		height: 160rpx;
		margin-top: 20%;
	}

	.pic image {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100%;
	}

	.tips {
		width: 90%;
		font-family: 28rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #676a6d;
		margin-top: 160rpx;

	}

	.btn {
		background: linear-gradient(to right, #c8a357, #fbde94, #c8a357);
		width: 94%;
		height: 80rpx;
		line-height: 80rpx;
		margin: 0 auto;
		margin-top: 60rpx;
		border-radius: 16rpx;
		color: #333;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	::v-deep .u-button--primary {
		border: none !important;
		// background: linear-gradient(to right, #d5ad57, #cfb58a);
		background-color: $global-color-golden;
	}
</style>